const path = require("path");
const resolve = dir => path.join(__dirname, dir);
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']
 
module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? '/blog' : '/blog',
    assetsDir: 'static',
    outputDir: 'dist/blog',
    runtimeCompiler: true,
    pages: {
        blog: { entry: 'src/main.js' }
    },
    chainWebpack: config => {
        config.resolve.alias
            .set("@", resolve("src"))
            .set("@assets", resolve("src/assets"))
            .set("@components", resolve("src/components"))
            .set("@views", resolve("src/views"))
            .set("@router", resolve("src/router"))
            .set("@store", resolve("src/store"))
            .set("@request", resolve("src/request"))

        config.when(process.env.NODE_ENV !== 'development', (config) => {
            config
                .plugin('compression')
                .use(CompressionWebpackPlugin, [
                    {
                        filename: '[path][base].gz[query]',
                        algorithm: 'gzip',
                        test: new RegExp(
                            '\\.(' + productionGzipExtensions.join('|') + ')$'
                        ),
                        threshold: 200,// 8192
                        minRatio: 0.8,
                    },
                ])
                .end()
        })
    },
    css: {
        loaderOptions: {
            less: {
                lessOptions: {
                    modifyVars: {
                        'primary-color': '#1DA57A',
                        'link-color': '#1DA57A',
                        'border-radius-base': '2px',
                    },
                    javascriptEnabled: true,
                },
            },
        },
    },
    devServer: {
        hot: true,
        port: 9999,
        open: false,
        noInfo: false,
        proxy: {
            "/api": {
                target: "http://localhost:7001",
                changeOrigin: true, // 是否跨域
                // secure: true,
                pathRewrite: {
                    '^/api': ''
                }
            },
            // "/resources": {
            //     target: "http://122.152.195.222/resources",
            //     changeOrigin: true,
            //     secure: false,
            //     pathRewrite: {
            //         "^/resources": ''
            //     }
            // }
        }
    }
}
